//
//  DPPictureSelectorCell.m
//  DailyPhoto
//
//  Created by 黄凯展 on 16/7/25.
//  Copyright © 2016年 ShengCheng. All rights reserved.
//

#import "DPPictureSelectorCell.h"
#import "Masonry.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "NSObject+DP.h"
#import "BMPhotoIOTool.h"

@interface DPPictureSelectorCell ()

/***  背景图片按钮*/
@property (nonatomic, weak) UIButton *backButton;
/***  打钩按钮*/
@property (nonatomic, weak) UIButton *checkButton;

@end

@implementation DPPictureSelectorCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

// 子控件
- (void)setupSubviews
{
    [self setupBackButton];
    
    [self setupCheckButton];
}

- (void)setupBackButton
{
    __weak typeof (self) weakSelf = self;
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton addTarget:self action:@selector(backButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    backButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:backButton];
    self.backButton = backButton;
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf);
    }];
}

- (void)setupCheckButton
{
    __weak typeof (self) weakSelf = self;
    UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
    checkButton.userInteractionEnabled = NO;
    [checkButton setBackgroundImage:[UIImage imageNamed:@"DP_post_check"] forState:UIControlStateNormal];
    [checkButton setBackgroundImage:[UIImage imageNamed:@"DP_post_check_selected"] forState:UIControlStateSelected];
    [self.contentView addSubview:checkButton];
    self.checkButton = checkButton;
    [checkButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(SCREEN_WIDTH/375*(22.5));
        make.right.equalTo(weakSelf.contentView).offset(-7);
        make.top.mas_equalTo(7);
    }];
}

#pragma mark - 事件
- (void)backButtonDidClicked:(UIButton *)button
{
    if (self.imageButtonDidClickedBlock) {
        self.imageButtonDidClickedBlock(self.checkButton,self.photo);
    }
}

#pragma mark - 设置数据

- (void)setImage:(UIImage *)image
{
    _image = image;
    if (image) {
        [self.backButton setImage:image forState:UIControlStateNormal];
    }
}

- (void)setPhoto:(BMPhotoIOPhotoModel *)photo{
    _photo = photo;
    if (photo) {
        if (photo.asset) {
            if (photo.thumbImage && [photo.thumbImage isKindOfClass:[UIImage class]]) {
                [self.backButton setImage:photo.thumbImage forState:UIControlStateNormal];
//                self.checkButton.hidden = NO;
            }else{
                [[BMPhotoIOTool sharedTool] thumbImageWithAsset:photo.asset finishBlock:^(id obj) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if ([obj isKindOfClass:[UIImage class]]) {
                            [self.backButton setImage:(UIImage *)obj forState:UIControlStateNormal];
                            self.contentView.backgroundColor = [UIColor whiteColor];
                        }else{
                            [self.backButton setImage:[UIImage imageNamed:@"DP_post_camera"] forState:UIControlStateNormal];
                            self.contentView.backgroundColor = Color_RGB(226, 226, 226);
                        }
                    });
                }];
//                self.checkButton.hidden = YES;
            }
        }
        else if (photo.assetIOS7) {
            UIImage *image;
            if (photo.assetIOS7.thumbnail) {
                image = [UIImage imageWithCGImage:photo.assetIOS7.thumbnail];
                self.contentView.backgroundColor = [UIColor whiteColor];
                self.checkButton.hidden = NO;
            }
            [self.backButton setImage:image forState:UIControlStateNormal];
        }
        else{
            [self.backButton setImage:[UIImage imageNamed:@"DP_post_camera"] forState:UIControlStateNormal];
            self.contentView.backgroundColor = Color_RGB(226, 226, 226);
        }
        
        if (photo.isSelected) {
            self.checkButton.selected = YES;
        } else {
            self.checkButton.selected = NO;
        }
        // 相机隐藏勾选按钮
        self.checkButton.hidden = photo.type == BMPhotoIOPhotoTypeCamera;
    }
}

@end
